package com.zxjbyte.yiyi.framework.common.core.okhttp.retry;

import com.github.rholder.retry.RetryListener;
import com.google.common.base.Predicate;
import com.zxjbyte.yiyi.framework.common.core.okhttp.IResponseProcessor;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 重试策略
 *
 * @Author zhangxingjia
 * @Date 2024/3/16 13:11
 * @Version: 1.0
 */
@Data
@Accessors(chain = false)
public class RetryPolicy {

    // 重试条件根据指定异常类型重试
    private Class<? extends Throwable> exceptionClass;

    // 根据返回结果重试
    private Predicate<IResponseProcessor> resultPredicate;

    // 重试间隔：单位秒
    private Integer sleepTime;

    // 停止策略，最多重试次数
    private Integer attemptNumber;

    // 单次执行时间限制，单位秒
    private Integer duration;

    // 重试监听器
    private RetryListener listener;

    // 是否抛出RetryException异常
    private Boolean throwOnRetryFailure;

    // 是否抛出ExecutionException异常
    private Boolean throwOnExecutionFailure;

    public RetryPolicy(){
        this(0, 3);
    }

    public RetryPolicy(int sleepTime, int attemptNumber) {
        this(sleepTime, attemptNumber, Exception.class);
    }

    public RetryPolicy(int sleepTime, int attemptNumber, Class<? extends Throwable> exceptionClass) {
        this.sleepTime = sleepTime;
        this.attemptNumber = attemptNumber;
        this.exceptionClass = exceptionClass;
    }
}
